const a = { start: 3, end: 5 };
const b = { start: 7, end: 8 };
const c = { start: 4, end: 6 };

function isOverlapped (first, second) {
  const {start : f_start, end: f_end} = first
  const {start : s_start, end: s_end} = second

  if (f_start > s_start) {
    if (s_end >= f_start) {
      return true
    } else {
      return false
    }
  } else if (f_start === s_start) {
    return true
  } else {
    if (f_end > s_start){
      return true
    } else {
      return false
    }
  }
}

function compare(a, b) {
  return a.start - b.start
}

function merge(intervals) {
  intervals.sort(compare)
  let idx = 0
  while (intervals[idx + 1]) {
    if (isOverlapped(intervals[idx], intervals[idx + 1])) {
      const end = Math.max(intervals[idx].end, intervals[idx + 1].end)
      const _obj = {start: intervals[idx].start, end}
      intervals.splice(idx, 1, _obj)
      intervals.splice(idx + 1, 1)
      idx--
    } else {
      idx++
    }
  }
  return intervals

}

console.log(merge([a, b, c])) // [ { start: 3, end: 6 }, { start: 7, end: 8 } ]